from smtplib import SMTP_SSL
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
import time

"""
@:param kind 故障类型
"""


def send_mail(kind):
    message_for_admin = {7: "1. 检查物料C进料阀门是否漏液\n 2. 加大物料C进料泵功率\n",
                         8: "1. 检查/调整物料A进料阀门开启情况\n 2. 检查/调整惰性气体B进料速度\n 3. 检查/调整物料C进料阀门开启情况\n",
                         12: "检查/降低冷凝器器冷却水预设温度\n",
                         14: "检查/调整反应器冷却水阀门开启情况\n"}
    fault_type = {7:"物料C压力头损失", 8:"物料A,B,C的组成成分发生变化",12:"冷凝器冷却水入口温度发生变化",14:"反应器冷却水闸门"}
    host_server = 'smtp.qq.com'  # qq邮箱smtp服务器
    sender_qq = '3351019618@qq.com'  # 发件人邮箱
    pwd = 'eshflcjrefuidbdd'
    receiver = ['3351019618@qq.com']  # 收件人邮箱
    mail_title = '系统出现故障！'  # 邮件标题
    t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    tem = ''
    mail_content = '系统于' + t + '时出现故障\n故障类型为:' + fault_type[eval(kind)] + '\n'  # 邮件正文内容
    mail_content = mail_content + "维修建议如下：\n" + message_for_admin[eval(kind)]
    msg = MIMEMultipart()
    msg["Subject"] = Header(mail_title, 'utf-8')
    msg["From"] = sender_qq
    msg['To'] = ";".join(receiver)
    msg.attach(MIMEText(mail_content, 'plain', 'utf-8'))
    smtp = SMTP_SSL(host_server)  # ssl登录
    smtp.login(sender_qq, pwd)
    try:
        smtp.sendmail(sender_qq, receiver, msg.as_string())
    except Exception as e:
        print(e)
        return -1
    smtp.quit()
    return 0


if __name__ == '__main__':
    send_mail(12)
